﻿using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using Shweet.Repository;

namespace Shweet.Engine
{
    public class Teams
    {
        private readonly ITeams _teams;

        public Teams(ITeams teams)
        {
            Contract.Requires(teams != null);
            _teams = teams;
        }

        public List<Team> DiscoverShweetTeams()
        {
            Contract.Ensures(Contract.Result<List<Team>>() != null);

            List<Team> discoveredTeams = _teams.Discover();
            List<Team> followedTeams = _teams.GetAll();

            foreach (Team team in followedTeams)
            {
                if (discoveredTeams.Contains(team))
                    discoveredTeams.Find(t => t.Equals(team)).Following = true;
            }
            return discoveredTeams;
        }

        public void FollowShweetTeams(List<Team> teamsToFollow)
        {
            Contract.Requires(teamsToFollow != null);
            List<Team> _following = _teams.GetAll();

            List<Team> teamsToAdd = teamsToFollow.Except(_following).ToList();

            _teams.Add(teamsToAdd);
        }

        public void UnFollowShweetTeams(List<Team> teamsToUnFollow)
        {
            Contract.Requires(teamsToUnFollow != null);

            _teams.Remove(teamsToUnFollow);
        }

        public List<Team> GetFollowing()
        {
            Contract.Ensures(Contract.Result<List<Team>>() != null);

            return _teams.GetAll();
        }
    }
}